<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_nongren/model')->方法名();
*/
namespace jy_nongren;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function upmember($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$member = pdo_get('jy_nongren_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		if(empty($member)){
			$membernew=array(
				'uniacid' => $_SESSION['uniacid'],
				'mid' => $mid,
				'fromuser'=>$_SESSION['fromuser'],
				'createtime'=>time(),
			);
			pdo_insert('jy_nongren_member',$membernew);
			if($_SESSION['fromuser']){
				$ls_fromyou=$this->setting['ls_fromyou']?:5;
				if($ls_fromyou>0){
					$this->getls_renwu(6,$ls_fromyou);
				}
				$this->jiahaoyou($mid,$_SESSION['fromuser']);
			}
			return $membernew;
		}
	}
	public function getls_renwu($type=0,$fee=0,$jiid=0){
		$mid=$mid?:$_SESSION['uid'];
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		if($type>0){
			$isget=$this->pdo_get('lslog',array('type'=>$type,'createtime >'=>$daytime,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			if($isget){
				if($type!=4 && $type!=5){
					exi('您今日已领取过该粮食！','error');
				}
				return false;
			}
		}
		if(!$type){
			$msg='购买粮食'.$fee.'G';
		}elseif($type==1){
			$fee=$this->setting['ls_dayfee'];
			$msg='每日免费领粮食'.$fee.'G';
		}elseif($type==2){
			$fee=$this->setting['ls_signfee'];
			$msg='完成签到任务，领粮食'.$fee.'G';
		}elseif($type==3){
			$fee=$this->setting['ls_youfee'];
			$ls_younum=$this->setting['ls_younum']?:5;
			$looklognum=$this->pdo_count('looklog',array('createtime >'=>$daytime,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			if($ls_younum>$looklognum || !$looklognum){
				return false;
			}
			$msg='完成串门好友任务，领粮'.$fee.'G';
		}elseif($type==4){
			$fee=$this->setting['ls_shopfee'];
			$msg='完成浏览商城任务，领粮'.$fee.'G';
		}elseif($type==5){
			$fee=$this->setting['ls_logfee'];
			$msg='完成浏览浏览动态任务，领粮'.$fee.'G';
		}elseif($type==6){
			$fee=$this->setting['ls_fromyou'];
			$msg='成功邀请一位好友，领粮'.$fee.'G';
		}elseif($type==-1){
			$msg='喂养消耗'.$fee.'G';
		}elseif($type==-2){
			$msg='平台代投喂'.$fee.'G';
		}
		if(!$fee){
			return false;
		}
		$lslog=array(
			'uniacid' => $_SESSION['uniacid'],
			'mid'=>$mid,
			'fee'=>$fee,
			'msg'=>$msg,
			'type'=>$type,
			'createtime'=>time(),
		);
		pdo_insert('jy_nongren_lslog',$lslog);
		if($type>=0){
			pdo_update('jy_nongren_member',array('lsfee +='=>$fee),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		}elseif($type==-1){
			pdo_update('jy_nongren_member',array('lsfee -='=>$fee),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid));
		}
		return $msg;
	}
	public function up_looklog($youmid=0){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$isget=$this->pdo_get('looklog',array('youmid'=>$youmid,'createtime >'=>$daytime,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($isget){
			return false;
		}else{
			$looklog=array(
				'uniacid' => $_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'youmid'=>$youmid,
				'createtime'=>time(),
			);
			pdo_insert('jy_nongren_looklog',$looklog);
			$this->getls_renwu(3);
			return true;
		}
	}
	public function jiahaoyou($youmid=0,$mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if(!$youmid){
			return '请选择好友！';
		}
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$isget=$this->pdo_get('haoyou',array('youmid'=>$youmid,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
		if($isget){
			$this->delete('haoyou',array('youmid'=>$youmid,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid']));
			return '取消好友成功！';
		}else{
			$looklog=array(
				'uniacid' => $_SESSION['uniacid'],
				'mid'=>$mid,
				'youmid'=>$youmid,
				'createtime'=>time(),
			);
			pdo_insert('jy_nongren_haoyou',$looklog);
			return '加好友成功！';
		}
	}
	public function upfeelog($post=array(),$type=0){
		$data=array(
			'uniacid' => $_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'sn'=>get_ordersn('jy_nongren_feelog','sn'),
			'jiid'=>$post['jiid'],
			'price'=>$post['price'],
			'total'=>$post['total']?:1,
			'type'=>$type,
			'createtime'=>time(),
		);
		pdo_insert('jy_nongren_feelog',$data);
		$id=pdo_insertid();
		return $id;
	}
	public function addmyji($order=array()){
		for($i=0;$i<$order['total'];$i++){
			$count=$this->pdo_count('myji',array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']));
			$count++;
			$title='蛋鸡'.$count.'号';
			$data=array(
				'uniacid' => $_SESSION['uniacid'],
				'mid'=>$order['mid'],
				'sn'=>get_ordersn('jy_nongren_myji','sn'),
				'feeid'=>$order['id'],
				'price'=>$order['price'],
				'title'=>$title,
				'status'=>1,
				'weitime'=>time()-24*3600,
				'createtime'=>time(),
			);
			pdo_insert('jy_nongren_myji',$data);
			$data['id']=pdo_insertid();
			$this->addjilog($data,0);
			$this->model('cv')->up_renwu($data,1);
			$this->model('cv')->up_renwu($data,3);
		}
		return true;
	}
	public function addjilog($myji=array(),$type=0){
		$data=array(
				'uniacid' => $_SESSION['uniacid'],
				'mid'=>$myji['mid'],
				'jiid'=>$myji['id'],
				'msg'=>$this->jilog_type[$type],
				'price'=>$myji['fee'],
				'type'=>$type,
				'createtime'=>time(),
		);
		pdo_insert('jy_nongren_jilog',$data);
		if($type==4){
			pdo_update('jy_nongren_myji',array('jdtime'=>time(),'jd +='=>1),array('id'=>$myji['id'],'uniacid'=>$_SESSION['uniacid']));
			pdo_update('jy_nongren_member',array('jd +='=>1),array('mid'=>$myji['mid'],'uniacid'=>$_SESSION['uniacid']));
		}
		return true;
	}
	public function toweiji($jiid=0){
		$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$dj_dayls=$this->setting['dj_dayls']?:100;
		if($jiid){
			$myji=$this->pdo_get('myji',array('mid'=>$_SESSION['youmid']?:$_SESSION['uid'],'id'=>$jiid,'uniacid'=>$_SESSION['uniacid']));
			if($myji['isweitou']){
				exi('该【'.$myji['title'].'】蛋鸡目前已委托投喂！','error');
			}
			if($myji['status']!=1){
				exi('该【'.$myji['title'].'】蛋鸡目前的状态不能喂养！','error');
			}
			if($myji['weitime']>$daytime){
				exi('该【'.$myji['title'].'】蛋鸡今天已经喂养过了！无需再次喂养！','error');
			}
			$lsfee=$dj_dayls;
		}else{
			$noweiji=$this->pdo_count('myji',array('isweitou'=>0,'status'=>1,'mid'=>$_SESSION['youmid']?:$_SESSION['uid'],'weitime <'=>$daytime,'uniacid'=>$_SESSION['uniacid']));
			$lsfee=$noweiji*$dj_dayls;
		}
		$member=$this->pdo_get('member',array('mid'=>$_SESSION['youmid']?:$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		if($member['lsfee']<$lsfee){
			exi('当前粮食仅剩【'.$member['lsfee'].'G】！请先购买粮食！','error');
		}
		$msg=$this->getls_renwu(-1,$lsfee);
		$update=array('weitime'=>time(),'lsfee +='=>$lsfee,'lsday +='=>1);
		$dj_step1day=$this->setting['dj_step1day']?:30;
		$dj_step2day=$this->setting['dj_step2day']?:60;
		if($jiid){
			$item=$this->pdo_get('myji',array('id'=>$jiid,'uniacid'=>$_SESSION['uniacid']));
			if(!$item['step'] && $item['lsday']+1>=$dj_step1day){
				$update['step']=1;
			}elseif($item['step']==1 && $item['lsday']+1>=$dj_step2day){
				$update['step']=2;
			}
			$this->pdo_update('myji',$update,array('id'=>$jiid,'uniacid'=>$_SESSION['uniacid']));
			$myji=$this->pdo_get('myji',array('id'=>$jiid,'uniacid'=>$_SESSION['uniacid']));
			$myji['fee']=$lsfee;
			$this->addjilog($myji,1);
		}else{
			$list = pdo_getall('jy_nongren_myji',array('isweitou'=>0,'status'=>1,'mid'=>$_SESSION['youmid']?:$_SESSION['uid'],'weitime <'=>$daytime,'uniacid'=>$_SESSION['uniacid']));
			foreach($list as $item){
				if(!$item['step'] && $item['lsday']+1>=$dj_step1day){
					$update['step']=1;
				}elseif($item['step']==1 && $item['lsday']+1>=$dj_step2day){
					$update['step']=2;
				}
				$this->pdo_update('myji',$update,array('status'=>1,'id'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
				$item['fee']=$lsfee;
				$this->addjilog($item,1);
			}
		}
		return '喂养成功：'.$msg;
	}
}
?>